<?php
function getCategoryGoods(){
    return Cache::rememberForever('categoryGoods',function (){
        //已知一级分类,需要三级分类,以及三级分类下的商品
        $first = \App\Models\Classify::where('pid', 0)->get();
    
        foreach ($first as $k => $value) {
            $goods = \App\Models\Goods::get();
            $aa = [];
            foreach ($goods as $good) {
                if (in_array($value['id'], $good['classify_id'])) {
                    $aa[] = $good->toArray();
                }
            }
            $first[$k]['goods'] = $aa;
            $first[$k]['san'] = [];
            foreach (\App\Models\Classify::where('pid', $value->id)->get() as $kk => $item) {
                $san = \App\Models\Classify::where('pid', $item->id)->limit(1)->get();
                if ($san) {
                    $first[$k]['san'] = array_merge($first[$k]['san'], $san->toArray());
                }
            }
        }
        return $first;
    });
}

?>
